import { Global } from './../../Global/index';
/*
 * @Date: 2023-02-11 18:18:52
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-11 18:53:02
 * @FilePath: \A19\backend\src\api\Select\ApiSelectCourse.ts
 */
import { ApiCall } from "tsrpc";
import { ReqSelectCourse, ResSelectCourse } from "../../shared/protocols/Select/PtlSelectCourse";

export default async function (call: ApiCall<ReqSelectCourse, ResSelectCourse>) {
    // TODO
    const { _id: studentId } = call.currentUser
    const courseId = call.req.courseId

    Global.collection('selectCourses').findOne({
        course: courseId,
        student: studentId
    }).then(async res => {
        if (res !== null) {
            call.error('课程已经选择')
        } else {
            await Global.collection('selectCourses').insertOne({
                course: courseId,
                student: studentId
            })
            call.succ({})
        }
    })
}